<?php
declare(strict_types=1);

namespace Plugin\Alarm\Service;

use Plugin\Alarm\Repository\RdAlarmDangerRepository;
use App\Service\IService;
use Plugin\Alarm\Repository\RdAlarmWorkRepository;

final class RdAlarmDangerService extends IService
{
    public function __construct(
        protected readonly RdAlarmDangerRepository $repository,
        protected readonly RdAlarmWorkService $workService
    ) {}

    public function Report(array $data)
    {
//        记录隐患信息
        $danger = $this->create([
            'type' => $data['type'],
            'stand_id' => $data['stand_id'],
            'camera_id' => $data['camera_id'] ?? 0,
            'addr' => $data['addr'] ?? '',
            'from_type' => $data['from_type'] ?? 3,
            'severity' => $data['severity'],
            'extra' => $data['extra'] ?? [],
            'created_by' => $data['created_by'],
        ]);
//        提报工单
        if ($this->workService->workCreate([
            'title' => $data['title'],
            'content' => $data['content'],
            'stand_id' => $data['stand_id'],
            'type' => $data['work_type'],
            'expire_at' => $data['expire_at'],
            'priority' => $data['severity'],
            'related_type' => 2,
            'related_id' => $danger->id,
            'created_by' => $data['created_by'],
        ])) {
            $danger->update([
                'is_work' => 1
            ]);
        }
    }
}